package com.unicorn.modual.fts.service.impl;

import com.unicorn.base.VinaceConfig;
import com.unicorn.entity.enums.SupportFileType;
import com.unicorn.modual.fts.service.FtsService;
import com.unicorn.util.common.FileComponent;
import com.unicorn.vo.FileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 2016/12/3.
 */
@Service
public class FtsServiceImpl implements FtsService {

    @Autowired
    private FileComponent fileCompoment;


    @Override
    public FileVO createFileOrDirectory(SupportFileType fileType, String name, String parentPath) {
        if (parentPath == null) {
            parentPath = VinaceConfig.ONLINE_FILE_SYSTEM_ROOT;
        }
        if (name == null) {
            name = "";
        }
        FileVO fileVO = new FileVO();
        fileVO.setFile(Boolean.TRUE);
        fileVO.setFilePath(parentPath);
        if (fileVO.getFile()) {
            if (name == null) {
                name = "新建文件";
            }
            fileVO = swithFileType(fileVO);
            fileVO.setFileName(name);
            fileCompoment.createFile(fileVO);
        } else {
            if (name == null) {
                name = "新建目录";
            }
            fileVO.setFileName(name);
            fileCompoment.createDirectory(fileVO);
        }
        return fileVO;

    }

    protected FileVO swithFileType(FileVO fileVO) {

        if (SupportFileType.DIRECTORY.toString().equals(fileVO.getFileType())) {
            fileVO.setFile(Boolean.FALSE);
        } else if (SupportFileType.DOC.toString().equals(fileVO.getFileType())) {
            fileVO.setPerfix(".doc");
        } else if (SupportFileType.DOCX.toString().equals(fileVO.getFileType())) {
            fileVO.setPerfix(".docx");
        } else if (SupportFileType.XLS.toString().equals(fileVO.getFileType())) {
            fileVO.setPerfix(".xls");
        } else if (SupportFileType.XLSX.toString().equals(fileVO.getFileType())) {
            fileVO.setPerfix(".xlsx");
        } else if (SupportFileType.TXT.toString().equals(fileVO.getFileType())) {
            fileVO.setPerfix(".txt");
        } else {
            fileVO.setPerfix(".java");
        }
        return fileVO;
    }

    @Override
    public List<FileVO> listFiles(String parentPath) {
        if (parentPath == null) {
            parentPath = VinaceConfig.ONLINE_FILE_SYSTEM_ROOT;
        }
        List<FileVO> fileVOList = new ArrayList<FileVO>();
        File parentFile = new File(parentPath);
        if (parentFile.exists() && parentFile.isDirectory()) {
            List<File> files = fileCompoment.listFiles(parentPath);
            for (File file : files) {
                FileVO vo = new FileVO();
                vo.setFileName(file.getName());
                vo.setFilePath(file.getAbsolutePath());
                vo.setFile(file.isFile());
                if (file.isFile()) {
                    int idx = file.getName().indexOf(".");
                    vo.setPerfix(file.getName().substring(idx));
                }
                vo.setFileSize(file.getTotalSpace());
                fileVOList.add(vo);
            }
            return fileVOList;
        } else {
            throw new RuntimeException(parentFile.getAbsolutePath() + "不存在或不是目录");
        }

    }

    @Override
    public List<FileVO> listFiles(File parentFile) {
        if (parentFile.exists() && parentFile.isDirectory()) {
            List<File> files = fileCompoment.listFiles(parentFile.getAbsolutePath());
            List<FileVO> fileVOList = new ArrayList<FileVO>();
            for (File file : files) {
                FileVO vo = new FileVO();
                vo.setFileName(file.getName());
                vo.setFilePath(file.getAbsolutePath());
                if (file.isFile()) {
                    int idx = file.getName().indexOf(".");
                    vo.setPerfix(file.getName().substring(idx));
                }
                vo.setFileSize(file.getTotalSpace());
                fileVOList.add(vo);
            }
            return fileVOList;
        } else {
            throw new RuntimeException(parentFile.getAbsolutePath() + "不存在或不是目录");
        }
    }

    @Override
    public boolean deleteFileOrDirectory(String fileName, String parentPath) {

        if (parentPath == null) {
            parentPath = VinaceConfig.ONLINE_FILE_SYSTEM_ROOT;
        }

        return false;
    }
}
